package com.sc.outpatientsys.controller;

import com.sc.outpatientsys.entity.Diagnosis;
import com.sc.outpatientsys.service.IDiagnosisService;
import com.sc.outpatientsys.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/diagnosis")
public class DiagnosisController extends BaseController{

    @Autowired
    private IDiagnosisService diagnosisService;
    @RequestMapping("/addDiagnosis")
    public JsonResult<Void> insertDiagnosis(int cr_ID,String dg_info,int[] med_id){
        String med_id_List= Arrays.toString(med_id).replace("[","").replace("]","").replace(" ","");
        log.info("med_id_List"+"::"+med_id_List);
        Diagnosis diagnosis = new Diagnosis();
        diagnosis.setCr_ID(cr_ID);
        diagnosis.setDg_info(dg_info);
        diagnosis.setMed_ID_List(med_id_List);
        diagnosisService.insertDiagnosis(diagnosis);
        return new JsonResult<>(OK);
    }

    @RequestMapping("/findById")
    public JsonResult<Diagnosis> findDiagnosisById(int dg_ID){
        Diagnosis data = diagnosisService.findById(dg_ID);
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("/findByCr_Id")
    public JsonResult<Diagnosis> findByCR_ID(int cr_ID){
        Diagnosis data = diagnosisService.findByCR_ID(cr_ID);
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("/findAll")
    public JsonResult<List<Diagnosis>> findAll(){
        List<Diagnosis> data = diagnosisService.findAll();
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/update")
    public JsonResult<Void> update(int dg_ID,int cr_ID,String dg_info,int[] med_id){
        String med_id_string = Arrays.toString(med_id).replace("[", "").replace("]", "").replace(" ", "");
        Diagnosis diagnosis = new Diagnosis();
        diagnosis.setDg_ID(dg_ID);
        diagnosis.setCr_ID(cr_ID);
        diagnosis.setDg_info(dg_info);
        diagnosis.setMed_ID_List(med_id_string);
        diagnosisService.update(diagnosis);
        return new JsonResult<>(OK);
    }

    @RequestMapping("/deleteByCR_ID")
    public JsonResult<Void> delete1(int cr_ID){
        diagnosisService.delete(cr_ID);
        return new JsonResult<>(OK);
    }
    @RequestMapping("/deleteByDG_ID")
    public JsonResult<Void> delete2(int dg_ID){
        diagnosisService.deleteByDg_Id(dg_ID);
        return new JsonResult<>(OK);
    }
    @RequestMapping("/update_List")
    public JsonResult<Void> update_List(String med_id_list,int cr_ID){
        diagnosisService.update_List(med_id_list,cr_ID);
        return new JsonResult<>(OK);
    }


}
